package com.itwenke.springbootdemo.shirosimple.controller;

import com.itwenke.springbootdemo.shirosimple.demo.AbnormalMismatchDemo;
import com.itwenke.springbootdemo.shirosimple.demo.InnerClassDemo;
import com.itwenke.springbootdemo.shirosimple.demo.MultiThreadDemo;
import com.itwenke.springbootdemo.shirosimple.demo.NonBeanDemo;
import com.itwenke.springbootdemo.shirosimple.demo.PrivateMethodDemo;
import com.itwenke.springbootdemo.shirosimple.entity.BankAccount;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/bankAccount/test")
public class BankAccountTestController {

    @Resource
    private PrivateMethodDemo bankAccountDemo;

//    @Resource
//    private NonBeanDemo nonBeanDemo;

    @Resource
    private AbnormalMismatchDemo abnormalMismatchDemo;

    @Resource
    private MultiThreadDemo multiThreadDemo;

    @Resource
    private InnerClassDemo innerClassDemo;


    /**
     * 测试：私有方法private
     */
    @GetMapping("/privateMethod")
    public void privateMethod() throws Exception {
        // 访问不到私有方法
//        BankAccount bankAccount = new BankAccount();
//        bankAccount.setAccount("66666666");
//        bankAccount.setBalance(10000L);
//         bankAccountDemo.addBankAccount(bankAccount);
    }

    /**
     * 测试：目标类没有配置为Bean
     */
    @GetMapping("/nonBean")
    public void nonBean() throws Exception {
        NonBeanDemo nonBeanDemo = new NonBeanDemo();
        BankAccount bankAccount = new BankAccount();
        bankAccount.setAccount("88888888");
        bankAccount.setBalance(10000L);
        nonBeanDemo.addBankAccount(bankAccount);
    }

    /**
     * 测试：异常不匹配
     */
    @GetMapping("/abnormalMismatch")
    public void abnormalMismatch() throws Exception {
        BankAccount bankAccount = new BankAccount();
        bankAccount.setAccount("99999999");
        bankAccount.setBalance(10000L);
        abnormalMismatchDemo.addBankAccount(bankAccount);
    }

    /**
     * 测试：跨越多个线程，子线程抛异常，主线程正常
     */
    @GetMapping("/multiThread1")
    public void multiThread1() throws Exception {
        multiThreadDemo.addBankAccount1();
    }

    /**
     * 测试：跨越多个线程，主线程抛异常，子线程正常：
     */
    @GetMapping("/multiThread2")
    public void multiThread2() throws Exception {
        multiThreadDemo.addBankAccount2();
    }

    /**
     * 测试：跨越多个线程，解决方案
     */
    @GetMapping("/multiThread3")
    public void multiThread3() throws Exception {
        multiThreadDemo.addBankAccount3();
    }

    /**
     * 测试：内部类访问
     */
    @GetMapping("/innerClass")
    public void innerClass() throws Exception {
        BankAccount bankAccount = new BankAccount();
        bankAccount.setAccount("33333333");
        bankAccount.setBalance(10000L);
        innerClassDemo.testInnerClass(bankAccount);
    }
}
